/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gardengame_simple;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Student
 */
public class BoardGraphics extends JPanel{

    public BoardGraphics() {
        t = new Tile( false , false, true, 0, 0);
        t1 = new Tile (false, false, false, 7 , 1);
        t2 = new Tile (true, false, false, 0 , 2);
        t3 = new Tile (false, true, false, 0 , 3);
        tiles = new Tile[] {t,t1,t2,t3};
        bs = new BoardState(160,3,2,0,0,0,0);
        
        loadResources();
        this.setPreferredSize(new Dimension(bs.getPixels()*bs.getColumns(),bs.getPixels()*bs.getRows()));
    }
    
    private BoardState bs;
    private Tile[] tiles;
    private Tile t;
    private Tile t1;
    private Tile t2;
    private Tile t3;
    
    
    private BufferedImage rock;
    private BufferedImage bg;
    private BufferedImage critter;
    private BufferedImage flower;
    private BufferedImage bug;
    private BufferedImage scaledRock;
    private BufferedImage scaledBG;
    private BufferedImage scaledCritter;
    private BufferedImage scaledFlower;
    private BufferedImage scaledBug;
    
    private void loadResources() {
        //implement code
        bg = loadImage("src/gardengame_simple/bgTile.png");
        rock = loadImage("src/gardengame_simple/rock.png");
        critter = loadImage("src/gardengame_simple/hamtaro.png");
        bug = loadImage("src/gardengame_simple/bug.png");
        flower = loadImage("src/gardengame_simple/photo_daisy.png");
        
        scaledBG = resize(bg, bs.getPixels(), bs.getPixels());
        scaledRock = resize(rock, bs.getPixels(), bs.getPixels());
        scaledCritter = resize(critter, bs.getPixels(), bs.getPixels());
        scaledFlower = resize(flower, bs.getPixels(), bs.getPixels());
        scaledBug = resize(bug, bs.getPixels()/5, bs.getPixels()/5);
    }
    
    public void drawTiles(Graphics g) {
        for (Tile t : tiles){
            drawTile(t,g);
        }
    }
    
    private void drawTile(Tile t, Graphics g) {
        int xPos = getTileXFromIndex( t.getIndex() ) * bs.getPixels();
        int yPos = getTileYFromIndex( t.getIndex() ) * bs.getPixels();
        g.drawImage(scaledBG, xPos, yPos, this);
        if (t.isRock()){
            g.drawImage(scaledRock, xPos, yPos, this);
        } else if (t.isFlower()) {
            g.drawImage(scaledFlower, xPos, yPos, this);
        }
        if (t.isCritter()) {
            g.drawImage(scaledCritter, xPos, yPos, this);
        }
        if (t.getBugs() > 0){
            Random rand = new Random();
            for (int i = 0; i < t.getBugs(); i++){
                g.drawImage(scaledBug, xPos + rand.nextInt((int)(bs.getPixels()*.8)), yPos + rand.nextInt((int)(bs.getPixels()*.8)), this);
            }
        }
    }
    
    private int getTileXFromIndex( int index ){
        return index % bs.getColumns();
    }

    private int getTileYFromIndex( int index ){
        return index / bs.getColumns() ;
    }

    
    public static BufferedImage loadImage(String ref) {  
        BufferedImage bimg = null;  
        try {  
  
            bimg = ImageIO.read(new File(ref));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return bimg;  
    } 
    
    public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
        int w = img.getWidth();  
        int h = img.getHeight();  
        BufferedImage dimg = dimg = new BufferedImage(newW, newH, img.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
        g.dispose();  
        return dimg;  
    }
    
    @Override // in order to draw anything you need to override the paint component?
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D graphic2d = (Graphics2D) g;
        
        //paintTest( g);
        drawTiles( g ) ;
    }
}
